package com.tjn;

/**
 * @author 陶镓妮
 * @version 1.0
 * 字符串部分内容翻转练习
 */
public class Exercise01 {
    public static void main(String[] args) {
        String str = "abcdefg";
        System.out.println("====翻转前====");
        System.out.println(str);
        try {
            reverse(str,1,9);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return;
        }
        System.out.println("====翻转后====");
        System.out.println(str);
    }


    public static String reverse(String str, int start, int end){

        //对输入的参数进行判断
        if(!(str != null && start >= 0 && start <end && end < str.length())){
            throw new RuntimeException("参数输入有误");
        }

        char[] c = str.toCharArray();
        char temp = ' ';
        for(int i = start, j = end; i < j; i++, j--){
            temp = c[i];
            c[i] = c[j];
            c[j] = temp;
        }
        return new String(c);
    }
}
